﻿using System;
using DataAccess.Repository;

namespace KPI
{
	public partial class EndOfTheYearAppraisal : BasePage
	{
		//private IKPIProgressRepository _KPIProgressRepository;
		private IKPIRepository _KPIRepository;
		private DataAccess.Models.KPI _TheKPI;

		public int TheKPIID
		{
			get
			{
				return CommonHelper.QueryStringInt("kpiid");
			}
		}

		public DataAccess.Models.KPI TheKPI
		{
			get
			{
				if(_TheKPI == null && TheKPIID > 0)
				{
					if(_KPIRepository == null)
					{
						_KPIRepository = new DataAccess.Repository.KPIRepository();
					}

					_TheKPI = _KPIRepository.GetTheKPI(TheKPIID);
				}

				return _TheKPI;
			}
		}

		protected void Page_Init(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{
				var canAssessment = false;

				//var u = KPISiteContext.Current.CurrentUser;

				if(TheCurrentLoginUser != null && TheCurrentLoginUser.RoleName.Equals("administrators"))
				{
					canAssessment = ((TheKPI.CurrentState == DataAccess.Models.KPIStates.Progress_Report_End && TheKPI.FinishTime < DateTime.Now) ||
														TheKPI.CurrentState == DataAccess.Models.KPIStates.Year_End_Assessment_In_Progress);
				}

				if(!canAssessment)
				{
					Response.Redirect("~/KPIList.aspx?groupid=" + TheKPI.GroupID.ToString());
				}
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{
				BindData();

				CKFinder.FileBrowser _FileBrowser = new CKFinder.FileBrowser();
				_FileBrowser.BasePath = "/ckfinder/";
				_FileBrowser.DisableThumbnailSelection = true;
				_FileBrowser.SetupCKEditor(CKEditorEndOfTheYearAssessment);
			}
		}

		protected void cusvEndOfTheYearAssessment_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
		{
			try
			{
				args.IsValid = (args.Value.Length <= 1000);
			}
			catch
			{
				args.IsValid = false;
			}
		}

		protected void btnSubmit_Click(object sender, EventArgs e)
		{
			if(TheKPI != null && TheKPI.TheKPIProgress != null)
			{
				TheKPI.TheKPIProgress.EndOfTheYearAssessment = CKEditorEndOfTheYearAssessment.Text.Trim();
				TheKPI.TheKPIProgress.KPIProgressRemark = txtKPIProgressRemark.Text.Trim();
				TheKPI.TheKPIProgress.KPIProgressState = rblKPIProgressState.SelectedValue;

				TheKPI.PreState = TheKPI.CurrentState;
				TheKPI.CurrentState = DataAccess.Models.KPIStates.End;

				if(_KPIRepository.UpdateKPI(TheKPI, false))
				{
					AddLatestInfo("KPI管理平台消息：您有一条KPI指标已年底考核,结果为" + rblKPIProgressState.SelectedItem.Text + "。", TheKPI);

					Response.Redirect("~/KPIList.aspx?groupid=" + TheKPI.GroupID.ToString());
				}
				else
				{
					ShowMessage("出错了,请稍后再试或联系管理员.", true);
				}
			}
		}

		protected void btnCancel_Click(object sender, EventArgs e)
		{
			Response.Redirect("~/KPIList.aspx?groupid=" + TheKPI.GroupID.ToString());
		}

		protected void Page_UnLoad(object sender, EventArgs e)
		{
			//if(_KPIProgressRepository != null)
			//{
			//  ((IDisposable)_KPIProgressRepository).Dispose();
			//}

			if(_KPIRepository != null)
			{
				((IDisposable)_KPIRepository).Dispose();
			}
		}

		private void BindData()
		{
			if(TheKPI != null)
			{
				lGroup.Text = TheKPI.TheGroup != null ? TheKPI.TheGroup.GroupName : string.Empty;

				lKPIContent.Text = (TheKPI.KPIContent.Length > 11 ? TheKPI.KPIContent.Substring(0, 10) : TheKPI.KPIContent);
				lKPIContent_Full.Text = TheKPI.KPIContent;
				btnExpand_KPIContent.OnClientClick = "return btnExpand_click(this, '#" + cell_kpicontent_progress.ClientID.ToString() + "');";

				if(!string.IsNullOrWhiteSpace(TheKPI.TheKPIProgress.Quarter1Progress))
				{
					lQuarter1Progress.Text = (TheKPI.TheKPIProgress.Quarter1Progress.Length > 10 ? TheKPI.TheKPIProgress.Quarter1Progress.Substring(0, 9) : TheKPI.TheKPIProgress.Quarter1Progress);
					lQuarter1Progress_Full.Text = TheKPI.TheKPIProgress.Quarter1Progress;
					btnExpand_Quarter1.OnClientClick = "return btnExpand_click(this, '#" + cell_quarter1progress.ClientID.ToString() + "');";
				}
				else
				{
					btnExpand_Quarter1.Visible = false;
					cell_quarter1progress.InnerHtml = string.Empty;
				}

				if(!string.IsNullOrWhiteSpace(TheKPI.TheKPIProgress.Quarter2Progress))
				{
					lQuarter2Progress.Text = (TheKPI.TheKPIProgress.Quarter2Progress.Length > 10 ? TheKPI.TheKPIProgress.Quarter2Progress.Substring(0, 9) : TheKPI.TheKPIProgress.Quarter2Progress);
					lQuarter2Progress_Full.Text = TheKPI.TheKPIProgress.Quarter2Progress;
					btnExpand_Quarter2.OnClientClick = "return btnExpand_click(this, '#" + cell_quarter2progress.ClientID.ToString() + "');";
				}
				else
				{
					btnExpand_Quarter2.Visible = false;
					cell_quarter2progress.InnerHtml = string.Empty;
				}

				if(!string.IsNullOrWhiteSpace(TheKPI.TheKPIProgress.Quarter3Progress))
				{
					lQuarter3Progress.Text = (TheKPI.TheKPIProgress.Quarter3Progress.Length > 10 ? TheKPI.TheKPIProgress.Quarter3Progress.Substring(0, 9) : TheKPI.TheKPIProgress.Quarter3Progress);
					lQuarter3Progress_Full.Text = TheKPI.TheKPIProgress.Quarter3Progress;
					btnExpand_Quarter3.OnClientClick = "return btnExpand_click(this, '#" + cell_quarter3progress.ClientID.ToString() + "');";
				}
				else
				{
					btnExpand_Quarter3.Visible = false;
					cell_quarter3progress.InnerHtml = string.Empty;
				}

				if(!string.IsNullOrWhiteSpace(TheKPI.TheKPIProgress.Quarter4Progress))
				{
					lQuarter4Progress.Text = (TheKPI.TheKPIProgress.Quarter4Progress.Length > 10 ? TheKPI.TheKPIProgress.Quarter4Progress.Substring(0, 9) : TheKPI.TheKPIProgress.Quarter4Progress);
					lQuarter4Progress_Full.Text = TheKPI.TheKPIProgress.Quarter4Progress;
					btnExpand_Quarter4.OnClientClick = "return btnExpand_click(this, '#" + cell_quarter4progress.ClientID.ToString() + "');";
				}
				else
				{
					btnExpand_Quarter4.Visible = false;
					cell_quarter4progress.InnerHtml = string.Empty;
				}

			}
		}
	}
}